package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.DepartmentVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/categoryTree")
//    public ResponseBean categoryTree(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
    public ResponseBean categoryTree(HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        //获取分类的数据
        PageVO<ProductCategoryTreeNodeVO> pageInfo = productCategoryService.categoryTree(pageNum,pageSize);
        return ResponseBean.success(pageInfo);
    }
    //获取父级的分类信息
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(productCategoryTreeNodeVOS);
    }
    //添加分类
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated ProductCategoryVO productCategoryVO) throws BusinessException {
        productCategoryService.add(productCategoryVO);
        return ResponseBean.success();
    }
    //编辑分类
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductCategoryVO productCategoryVO = productCategoryService.edit(id);
        return ResponseBean.success(productCategoryVO);
    }
    //修改分类
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,@RequestBody @Validated ProductCategoryVO productCategoryVO) throws BusinessException {
        productCategoryService.update(id,productCategoryVO);
        return ResponseBean.success();
    }
    //删除分类信息
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws BusinessException {
        productCategoryService.delete(id);
        return ResponseBean.success();
    }
}


